﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.Algorithm;
using Microhard.Permission.Models;
using Microhard.Core.DBUtility;
using Microhard.Permission.Interface;

namespace Microhard.Permission.MVCFilter
{
    public class AfterSaveCreateActionFilter : IAfterActionDataFilter
    {

        #region IAfterActionDataFilter Members

        public void FilterAction(System.Web.Mvc.ActionExecutedContext filterContext, User currentUser)
        {
            //获得datapermission参数
            var rtn = filterContext.HttpContext.Items["executedResult"] as ExecutedResult;

            if (rtn != null && rtn.IsSucceed && rtn.ExecutedModel != null)
            {
                var bmp = rtn.ExecutedModel as BasePermissionModel;
                if (bmp.OperationStatus == OperationStatus.Add)
                {
                    var businessPKID = AlgCompare.GetPKID<ModelPKIDAttribute>(rtn.ExecutedModel);
                    var resID = AlgCompare.GetPKID<ModelRESIDAttribute>(rtn.ExecutedModel);

                    DataPermission dp = new DataPermission();
                    dp.DataId = businessPKID;
                    dp.ResourceID = resID;
                    if (currentUser != null)
                    {
                        dp.OperatorID = currentUser.ID;
                    }
                    else
                    {
                        dp.OperatorID = bmp.OperatorID;
                    }
                    DataPermission.Add(dp);
                }
            }
        }



        public IDataPermission DataPermission
        {
            set;
            get;
        }
        #endregion

    }
}
